#!/usr/local/bin/perl

use strict;
use lib '..';
use MR::AsyncHTTP;
use Data::Dumper;
use Time::HiRes;

my $a = MR::AsyncHTTP->new(connect_timeout=>1);

my ($t1, $t2, $t3, $tt, $tt1);

my @l1 = qw!http://mail.ru http://ya.ru http://rambler.ru http://example.org http://net.crap http://holodilnik.ru http://perl.org safdsf 
	http://vk.com/ http://www.liveinternet.ru/rating/ru/ http://www.avito.ru/ http://www.avito.ru/astrahanskaya_oblast http://www.kinopoisk.ru/
	http://www.kinopoisk.ru/news/ http://qip.ru/ http://auto.ru/ http://auto.ru/ http://auto.ru/ http://auto.ru/!;

my @l2 = qw!http://files.mail.ru/fdasf http://files.mail.ru/fdfgdasf http://files.mail.ru/fdgfasf http://files.mail.ru/fdasf 
	http://files.mail.ru/fsddasf http://files.mail.ru/fdsdasf http://files.mail.ru/fdasf http://files.mail.ru/fda!;

$tt = Time::HiRes::time();

foreach my $url (@l1) {
	$t1 = Time::HiRes::time();
	$a->send_get($url);
	$t2 = Time::HiRes::time();
	printf "%0.3f %s\n", $t2-$t1, $url;
}

$tt1 = Time::HiRes::time();

for(1..10) {
	$t1 = Time::HiRes::time();
	my @r = $a->poke;
	$t2 = Time::HiRes::time();
	printf "%d requests are ready: %s; poke time: %0.3f\n", scalar(@r), join(', ', map { $a->{req}->{$_}->{url} } @r), $t2-$t1;
	Time::HiRes::usleep(50_000);
}

print "Waiting all\n";
$t1 = Time::HiRes::time();
$a->wait_all;
$t2 = Time::HiRes::time();
printf "Waitall time: %0.3f\n", $t2-$t1;

#print Dumper($a->{req});
my @r = $a->poke;
printf "%d requests are ready: %s\n", scalar(@r), join(', ', map { $a->{req}->{$_}->{url} } @r);

printf "Total time: %0.3f; Total async time: %0.3f\n", Time::HiRes::time() - $tt, Time::HiRes::time() - $tt1;
